package com.supplychainapi.utils;


import javax.servlet.http.Cookie;

/**
 * cookie工具
 */
public class CookieUtil {
    public static Cookie getCookie(String token) {
        Cookie cookie = new Cookie("token", token);
        cookie.setMaxAge(980000);  // (s)
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        return cookie;
    }

    public static Cookie getCookie(String key, String token) {
        Cookie cookie = new Cookie(key, token);
        cookie.setMaxAge(980000);  // (s)
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        return cookie;
    }

    public static Cookie getRemoveCookie(String token) {
        Cookie cookie = new Cookie("token", token);
        cookie.setMaxAge(0);  // (s)
        cookie.setDomain("localhost");
        cookie.setPath("/");
        return cookie;
    }

    /**
     * 获取jwt token
     * @param cookies
     * @return
     */
    public static String getJwtToken(Cookie[] cookies){
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    return cookie.getValue();
                }
            }
        }
        return "";
    }
}
